package cn.iocoder.yudao.module.enterprise.controller.admin.pdfImport;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.IOUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - PDF表单导入")
@RestController
@RequestMapping("/enterprise/pdfImport")
@Validated
public class PdfImportController {
    private static final String BASE_URL = "http://127.0.0.1:7863";
    private final RestTemplate restTemplate = new RestTemplate();
    private static final String UPLOAD_FOLDER = "D:\\Project\\PythonProject\\pdf\\res\\pdf\\"; //上传文件存放目录

    @PostMapping("/upload_file")
    @Operation(summary = "上传pdf文件")
    @PermitAll
    public CommonResult<Object> uploadFile(@RequestParam("file") MultipartFile file) {
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", file.getResource());
        // 2. 执行请求
        ResponseEntity<Object> exchange = restTemplate.exchange(
                BASE_URL + "/upload",
                HttpMethod.POST,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<Object>() {
                }); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        System.out.println(exchange.getBody());
        return success(exchange.getBody());
    }

    @GetMapping("/delete_file")
    @Operation(summary = "删除指定pdf文件")
    @PermitAll
    public CommonResult<Object> deleteFile(@RequestParam("file") String file) {

        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        // body.add("file", file);
        // 2. 执行请求
        ResponseEntity<Object> exchange = restTemplate.exchange(
                BASE_URL + "/delete_file?file={file}",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<Object>() {
                },
                file); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return success(exchange.getBody());
    }

    @GetMapping("/pdf_list")
    @Operation(summary = "获取pdf路径")
    @PermitAll
    public CommonResult<Object> pdfList() {
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//        addClientHeader(headers);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        // 2. 执行请求
        ResponseEntity<Object> exchange = restTemplate.exchange(
                BASE_URL + "/pdf_list",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<Object>() {
                }); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return success(exchange.getBody());
    }

    @GetMapping("/pdf_viewer")
    @Operation(summary = "预览pdf文件")
    @PermitAll
    public void pdfViewer(@RequestParam("file") String fileName, HttpServletResponse response) {
//        // 1.1 构建请求头
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        // 1.2 构建请求参数
//        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
//        // 2. 执行请求
//        ResponseEntity<InputStreamResource> exchange = restTemplate.exchange(
//                BASE_URL + "/pdf_viewer?file={file}",
//                HttpMethod.GET,
//                new HttpEntity<>(body, headers),
//                new ParameterizedTypeReference<InputStreamResource>() {
//                },
//                file); // 解决 CommonResult 的泛型丢失
//        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
//        return success(exchange.getBody());
        try {
            String filePath = UPLOAD_FOLDER + fileName;
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            response.setHeader("Content-Type", "application/pdf");
            OutputStream outputStream = response.getOutputStream();
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/pdf_viewer2")
    @Operation(summary = "预览pdf文件2")
    @PermitAll
    public CommonResult<MultipartFile> pdfViewer2(@RequestParam("file") MultipartFile file) {
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", file.getResource());
        // 2. 执行请求
        ResponseEntity<CommonResult<MultipartFile>> exchange = restTemplate.exchange(
                BASE_URL + "/pdf_viewer2",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<CommonResult<MultipartFile>>() {
                }); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return exchange.getBody();
    }

    @GetMapping("/deal_pdf")
    @Operation(summary = "处理pdf文件")
    @PermitAll
    public CommonResult<Object> dealPdf(@RequestParam("file") String file) {

        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        // 2. 执行请求
        ResponseEntity<Object> exchange = restTemplate.exchange(
                BASE_URL + "/deal_pdf?file={file}",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<Object>() {
                },
                file); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return success(exchange.getBody());
    }

    @GetMapping("/dealed_pdf")
    @Operation(summary = "获取所有已处理的pdf文件")
    @PermitAll
    public CommonResult<Object> dealedPdf() {

        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        // 2. 执行请求
        ResponseEntity<Object> exchange = restTemplate.exchange(
                BASE_URL + "/dealed_pdf",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<Object>() {
                }); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return success(exchange.getBody());
    }



}
